//
//  PublishPhotoCell.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/14.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

protocol PublishPhotoCellDelegate: NSObjectProtocol {
    func deleteImage(_ cell: PublishPhotoCell)
}

class PublishPhotoCell: UICollectionViewCell {
    
    weak var delegate: PublishPhotoCellDelegate?
    
    @IBOutlet private weak var photoView: UIImageView! {
        didSet {
            photoView.contentMode = .scaleAspectFill
            photoView.layer.cornerRadius = 8
            photoView.layer.masksToBounds = true
        }
    }
    @IBOutlet private weak var closeBtn: UIButton!
    
    func configure(with vm: PublishViewModel, indexPath: IndexPath) {
        if indexPath.item == vm.models.count {
            photoView.image = UIImage(named: "plus")
            closeBtn.isHidden = true
        } else {
            let imgVM = vm.viewModel(for: indexPath)
            photoView.kf.setImage(with: URL(string: imgVM.originalPath), placeholder: UIImage(named: "chat_image_placeholder"), completionHandler:  { result in
                if indexPath.item == vm.models.count {
                    self.photoView.image = UIImage(named: "plus")
                }
            })
            closeBtn.isHidden = false
        }
    }
    
    @IBAction private func deleteImage() {
        delegate?.deleteImage(self)
    }

}
